package logic

import (
	"context"
	"doctor-medicine-system/models/mysql"
	"errors"

	"doctor-medicine-system/docrpc/docrpc"
	"doctor-medicine-system/docrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type DrugAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDrugAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DrugAddLogic {
	return &DrugAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *DrugAddLogic) DrugAdd(in *docrpc.DrugAddRequest) (*docrpc.DrugAddResponse, error) {
	// todo: add your logic here and delete this line
	var doctoc mysql.DocToc

	if err := doctoc.FIndDocID(mysql.DB, in.DocTocId); err != nil {
		return nil, errors.New("医生id不存在")
	}
	drug := mysql.Drug{
		Model:      gorm.Model{},
		Norms:      in.Norms,
		Vendor:     in.Vendor,
		Indication: in.Indication,
		Dosage:     in.Dosage,
		Matter:     in.Matter,
		DocTocId:   in.DocTocId,
	}

	if err := drug.DrugAdd(mysql.DB); err != nil {
		return nil, err
	}
	return &docrpc.DrugAddResponse{
		DurgId: int64(drug.ID),
	}, nil
}
